class Commitment{
    static PENDING='待定';static FULFILLED='成功'; static REJECTED='拒绝';
    constructor(func){
        this.status=Commitment.PENDING;
        this.result=null;
        this.resolveCallbacks=[];
        this.rejectCallbacks=[];
        try{
            func(this.resolve.bind(this),this.reject.bind(this));
        }catch(error){
            this.reject(error)
        }
    }
    resolve(result){
        setTimeout(()=>{
            if(this.status===Commitment.PENDING){
                this.status=Commitment.FULFILLED;
                this.result=result;
                this.resolveCallbacks.forEach(callback=>{
                    callback(result);//回调函数//异步
                });
            }
        });
    }
    reject(result){
        setTimeout(()=>{
            if(this.status===Commitment.PENDING){
                this.status=Commitment.REJECTED;
                this.result=result;
                this.rejectCallbacks.forEach(callback=>{
                    callback(result);
                });
            }
        });
    }
    then(onFULFILLED,onREJECTED){
        return new Commitment((resolve,reject)=>{
            onFULFILLED=typeof onFULFILLED==='function'?onFULFILLED:()=>{};
            onREJECTED=typeof onREJECTED==='function'?onREJECTED:()=>{};
            if(this.status===Commitment.PENDING){
                this.rejectCallbacks.push(onFULFILLED);
                this.rejectCallbacks.push(onREJECTED);
            }
            if(this.status===Commitment.FULFILLED){
                setTimeout(()=>{
                    onFULFILLED(this.result);
                });
            }
            if(this.status===Commitment.REJECTED){
                setTimeout(()=>{
                    onREJECTED(this.result);
                });
            }
        });
    }
}
//promise的链式功能，then后面又有一个then，需要让then方法返回一个新的promise方法
// 返回一个新的promise之后他就有自己的then方法
let commitment=new Commitment((resolve,reject)=>{
    resolve("这次一定");
});
commitment.then(
    result=>{console.log(result)},
        result=>{console.log(result.massage)}
    
).then(
    result=>{console.log(result)},
    result=>{console.log(result.massage)}
);